/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-05-26     liuchao       the first version
 */
#ifndef APPLICATIONS_UTILS_H_
#define APPLICATIONS_UTILS_H_

/* Count the number of elements in an array. The array must be defined
 * as such; using this with a dynamically allocated array will give
 * incorrect results.
 */
#define LWM_N_ELEMENTS(arr) (sizeof(arr) / sizeof((arr)[0]))

#define LWM_UNUSED(x) (void)(x)

/* a=target variable, b=bit number to act upon 0-n */
#define LWM_BIT_SET(a, b) ((a) |= (1ULL << (b)))
#define LWM_BIT_CLEAR(a, b) ((a) &= ~(1ULL << (b)))
#define LWM_BIT_FLIP(a, b) ((a) ^= (1ULL << (b)))
#define LWM_BIT_CHECK(a, b) \
  (!!((a) & (1ULL << (b))))  // '!!' to make sure this returns 0 or 1

#define LWM_BITMASK_SET(x, mask) ((x) |= (mask))
#define LWM_BITMASK_CLEAR(x, mask) ((x) &= (~(mask)))
#define LWM_BITMASK_FLIP(x, mask) ((x) ^= (mask))
#define LWM_BITMASK_CHECK_ALL(x, mask) (!(~(x) & (mask)))
#define LWM_BITMASK_CHECK_ANY(x, mask) ((x) & (mask))

#endif /* APPLICATIONS_UTILS_H_ */
